package com.example.backend_422.controller;


import com.example.backend_422.common.Result;
import com.example.backend_422.service.UserService;
import com.example.backend_422.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwyzsn
 * @since 2023-04-22
 */
@RestController // @RestController = @Controller + @ResponseBody
@RequestMapping("/user") // URL地址映射
@CrossOrigin(origins = "*",maxAge = 3600) // 允许跨域
public class UserController {

    @Autowired // 注入Service
    private UserServiceImpl userService;

    @GetMapping("getall") // URL地址映射，获取所有用户
    public Result getAll(){
        return userService.getAll();
    }

    @PostMapping("login") // URL地址映射，登录
    // 接受前端传来的参数，参数名为username，password，类型为String
    public Result login(@RequestParam("username") String username, @RequestParam("password") String password){
        return userService.login(username,password);
    }

    @PostMapping("adduser")// URL地址映射，添加用户
    // 接受前端传来的参数，参数名为username，password，tel，role，类型为String
    // role为用户角色,
    public Result addUser(@RequestParam("username") String username,@RequestParam("password") String password
    ,@RequestParam("tel") String tel, @RequestParam("role") String role){
        return userService.addUser(username,password,tel,role);
    }
}
